package me.shurufa.widget.popupwindow;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.os.Handler;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.ScaleAnimation;
import android.widget.AbsoluteLayout;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;

import net.qiujuer.genius.blur.StackBlur;

import java.util.List;

import me.shurufa.R;
import me.shurufa.model.Catalog;
import me.shurufa.utils.LogUtils;
import me.shurufa.widget.wheelpicker.core.AbstractWheelPicker;
import me.shurufa.widget.wheelpicker.view.WheelCurvedPicker;


/**
 * 滚轮选择器popupWindow
 *
 * @FileName: me.khrystal.widget.WheelPopWindow.java
 * @author: kHRYSTAL
 * @email: 723526676@qq.com
 * @date: 2016-01-13 12:33
 */
public class WheelPopWindow extends PopupWindow
    implements
      AbstractWheelPicker.OnWheelChangeListener, View.OnClickListener {
  private View rootView;
  private View popRoot;
  private View btnSubmit, btnCancel;
  private static final String TAG_SUBMIT = "submit";
  private static final String TAG_CANCEL = "cancel";
  private List<Catalog> mData;
  private WheelCurvedPicker mPicker;
  private Catalog currentData;
  private Activity mContext;
//  blur
  private int mWidth;
  private int mHeight;
  private int statusBarHeight ;
  private Bitmap mBitmap= null;
  private Bitmap overlay = null;

  private Handler mHandler = new Handler();
  private Animation mShowAnim;
  private Button mButton;
  protected OnCompleteClickListener onCompleteClickListener;

  public WheelPopWindow(Context context, List<Catalog> data) {
    super(context);
    mContext = (Activity)context;
    mData = data;
    this.setWidth(ViewGroup.LayoutParams.FILL_PARENT);
    this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
    this.setBackgroundDrawable(new BitmapDrawable());
    this.setOutsideTouchable(true);
    mShowAnim = AnimationUtils.loadAnimation(context,R.anim.wheelpicker_anim_enter_bottom);
    LayoutInflater mLayoutInflater = LayoutInflater.from(context);
    rootView = mLayoutInflater.inflate(R.layout.pw_wheel, null);
    popRoot = rootView.findViewById(R.id.pop_root_layout);
    mPicker = (WheelCurvedPicker) rootView.findViewById(R.id.pop_wheel_curved);
    if (mPicker != null) {
      mPicker.setData(data);
      mPicker.setOnWheelChangeListener(this);
    }
    mButton = (Button)rootView.findViewById(R.id.right_button);
    mButton.setOnClickListener(this);
    popRoot.setOnClickListener(this);
    rootView.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        WheelPopWindow.this.dismiss();
      }
    });
    setContentView(rootView);

  }

  @Override
  public void onWheelScrolling(float deltaX, float deltaY) {

  }

  @Override
  public void onWheelSelected(int index, Catalog data) {
    currentData = data;
    LogUtils.d("WheelPopWindow",currentData.title);
  }

  @Override
  public void onWheelScrollStateChanged(int state) {

  }

  public void init(){
    Rect frame = new Rect();
    mContext.getWindow().getDecorView()
            .getWindowVisibleDisplayFrame(frame);
    statusBarHeight = frame.top;
    LogUtils.d("EditDigest", "" + statusBarHeight);
    DisplayMetrics metrics = new DisplayMetrics();
    mContext.getWindowManager().getDefaultDisplay()
            .getMetrics(metrics);
    mWidth = metrics.widthPixels;
    mHeight = metrics.heightPixels;

    setWidth(mWidth);
    setHeight(mHeight);
    mShowAnim.setFillAfter(true);
    mShowAnim.setDuration(500);
  }

  private Bitmap blur(int i) {
    long startMs = System.currentTimeMillis();
    // Is scale
    View view = mContext.getWindow().getDecorView();
    view.setDrawingCacheEnabled(true);
    view.buildDrawingCache(true);
    mBitmap = view.getDrawingCache();
    //TODO 去掉通知栏
    int width = mBitmap.getWidth();
    int height =  mBitmap.getHeight();
    mBitmap = Bitmap.createBitmap(mBitmap,0,statusBarHeight,width,height-statusBarHeight);
    height -= statusBarHeight;
    float scaleFactor = 8;//缩放比例
    float radius = 3;//模糊程度

    overlay = Bitmap.createBitmap((int) (width / scaleFactor),(int) (height / scaleFactor),Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(overlay);
    canvas.scale(1 / scaleFactor, 1 / scaleFactor);
    Paint paint = new Paint();
    paint.setFlags(Paint.FILTER_BITMAP_FLAG);
    canvas.drawBitmap(mBitmap, 0, 0, paint);
    //is blur
    // Java
    if (i == 1)
      overlay = StackBlur.blur(overlay, (int) radius, false);
      // Bitmap JNI Native
    else if (i == 2)
      overlay = StackBlur.blurNatively(overlay, (int) radius, false);
      // Pixels JNI Native
    else if (i == 3)
      overlay = StackBlur.blurNativelyPixels(overlay, (int) radius, false);

    LogUtils.d("EditDigestWindow", "blur time is:" + (System.currentTimeMillis() - startMs));
    return overlay;
  }

  public void showPickerWindow(View anchor,int bottomMargin) {
    android.widget.RelativeLayout.LayoutParams params =
            new android.widget.RelativeLayout.LayoutParams(AbsoluteLayout.LayoutParams.WRAP_CONTENT,
                    AbsoluteLayout.LayoutParams.WRAP_CONTENT);
    params.bottomMargin = bottomMargin;
    params.topMargin = 200;
    params.leftMargin = 18;
    popRoot.setAnimation(mShowAnim);
    //TODO 设置内部控件 Anim
    setAnimationStyle(android.R.style.Animation_Toast);
    //设置背景高斯模糊
    setBackgroundDrawable(new BitmapDrawable(mContext.getResources(), blur(2)));
    //外部是否可以点击
    setOutsideTouchable(true);
    setFocusable(true);
    showAtLocation(anchor, Gravity.BOTTOM, 0, statusBarHeight);
    popRoot.startAnimation(mShowAnim);
  }

  public Catalog getCurrentData() {
    return currentData;
  }

  public void setOnCompleteClickListener(OnCompleteClickListener listener){
    this.onCompleteClickListener = listener;
  }

  @Override
  public void onClick(View v) {
    switch (v.getId()){
      case R.id.pop_root_layout:
        break;
      case R.id.right_button:
        if (onCompleteClickListener!=null){
          onCompleteClickListener.onCompleteClicked();
        }
        break;
    }
  }

  public interface OnCompleteClickListener{
    void onCompleteClicked();
  }
}
